package jmine.tec.services.api.export;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.proxy.HibernateProxyHelper;

/**
 * Chave que identifica um exportador pela classe de objetos que ele exporta e tipo de ação de exportação.
 * 
 * @author lundberg
 */
public final class ExporterKey {
    private final Class<?> targetClass;

    private final ExportAction action;

    /**
     * Construtor
     * 
     * @param targetClass targetClass
     * @param action action
     */
    public ExporterKey(ExportAction action, Class<?> targetClass) {
        this.targetClass = targetClass;
        this.action = action;
    }

    /**
     * Construtor
     * 
     * @param action action
     * @param entity entity
     */
    public ExporterKey(ExportAction action, Object entity) {
        this(action, HibernateProxyHelper.getClassWithoutInitializingProxy(entity));
    }

    /**
     * Construtor
     * 
     * @param target target
     */
    public ExporterKey(ExportTarget target) {
        this(target.getAction(), target.getEntity());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.targetClass).append(this.action).toHashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj || obj == null || this.getClass() != obj.getClass()) {
            return true;
        }
        ExporterKey other = (ExporterKey) obj;
        return new EqualsBuilder().append(this.targetClass, other.targetClass).append(this.action, other.action).isEquals();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return String.format("ExporterKey[%s,%s]", this.action.toString(), this.targetClass.getName());
    }
}
